package medication_record

import (
	rich_err "app/error"
	"app/models"
	"app/service"
	"app/utils"
	"errors"
	"math"
	"strconv"

	"github.com/jinzhu/gorm"

	"github.com/gin-gonic/gin"
)

func Gets(context *gin.Context) ([]*models.MedicationRecordDetail, *models.Pager, *rich_err.RichError) {
	base := models.Orm.Model(&models.MedicationRecord{}).
		Select("medication_record.*, greenhouse.name as greenhouse_name,  orders.name as orders_name").
		Joins("LEFT JOIN greenhouse ON medication_record.greenhouse_id = greenhouse.id").
		Joins("LEFT JOIN orders ON medication_record.order_id = orders.id")

	results, pager, err := service.GetDetails[models.MedicationRecordDetail](context, base, models.MedicationRecord{}.TableName(), map[string]string{"greenhouse_name": "greenhouse.name", "orders_name": "orders.name"})

	if err != nil {
		return nil, nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	return results, pager, nil
}

func Get(id string) (*models.MedicationRecordDetail, *rich_err.RichError) {
	base := models.Orm.Model(&models.MedicationRecord{}).
		Select("medication_record.*, greenhouse.name as greenhouse_name,  orders.name as orders_name").
		Joins("LEFT JOIN greenhouse ON medication_record.greenhouse_id = greenhouse.id").
		Joins("LEFT JOIN orders ON medication_record.order_id = orders.id")

	result, err := service.GetDetail[models.MedicationRecordDetail](id, base)

	if err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	return result, nil
}

func Create(context *gin.Context) (*models.MedicationRecordDetail, *rich_err.RichError) {
	req := new(models.MedicationRecordCore)
	if err := context.ShouldBindJSON(req); err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	data := models.MedicationRecord{MedicationRecordCore: *req}
	result := models.Orm.Create(&data)
	if result.Error != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, result.Error)
	}
	return Get(strconv.Itoa(data.ID))
}

func Update(context *gin.Context) (*models.MedicationRecordDetail, *rich_err.RichError) {
	req := new(models.MedicationRecordDelta)
	if err := context.ShouldBindJSON(req); err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}

	data, err := service.Get[models.MedicationRecord](context)
	if err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	if req.OrderID != nil {
		data.OrderID = int(math.Round(req.OrderID.(float64)))
	}
	if req.GreenhouseID != nil {
		data.GreenhouseID = int(math.Round(req.GreenhouseID.(float64)))
	}
	if req.MedicationDate != nil {
		data.MedicationDate = utils.ToDateV2(req.MedicationDate.(string))
	}
	if req.Content != nil {
		data.Content = req.Content.(string)
	}

	if req.Remark != nil {
		data.Remark = req.Remark.(string)
	}
	result := models.Orm.Save(data)
	if result.Error != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, result.Error)
	}
	return Get(strconv.Itoa(data.ID))
}

func Delete(context *gin.Context) *rich_err.RichError {
	data, err := service.Get[models.MedicationRecord](context)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil
		}
		return rich_err.NewError(rich_err.RequestInvalid, err)
	}
	result := models.Orm.Delete(data)
	if result.Error != nil {
		return rich_err.NewError(rich_err.DatabaseFailed, result.Error)
	}
	return nil
}
